﻿using System;
using System.Diagnostics;
using System.IO;

namespace Msngr
{
    public static class Log
    {
        static bool doLog;
        static string logFile;

        public static bool DoLog
        {
            set { doLog = value; }
        }

        public static string LogFile
        {
            get { return logFile; }
            set { logFile = value; }
        }

        static Log()
        {
            doLog = true;
            logFile = "Log.txt";
        }

        public static void Write(string message, string sender)
        {
            if (doLog)
            {
                string stampedLog;
                string dateTime = DateTime.Now.ToString("dd/MM/yy HH:mm");
                if (sender == "") stampedLog = "[" + dateTime + "]" + message;
                else stampedLog = "[" + sender + "][" + dateTime + "]" + message;
                WriteToDisk(stampedLog);
                if (Debugger.IsAttached) Debug.WriteLine(stampedLog);
                Console.WriteLine(stampedLog);
                Console.ForegroundColor = ConsoleColor.Gray;
            }
        }

        public static void Write(string message)
        {
            Write(message, "");
        }

        public static void WriteInfo(string message, string sender)
        {
            Console.ForegroundColor = ConsoleColor.Blue;
            Write("Info: " + message, sender);
        }

        public static void WriteInfo(string message)
        {
            WriteInfo(message, "");
        }

        public static void WriteWarning(string message, string sender)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Write("Warning: " + message, sender);
        }

        public static void WriteWarning(string message)
        {
            WriteWarning(message, "");
        }

        public static void WriteError(string message, string sender)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Write("Error: " + message, sender);
        }

        public static void WriteError(string message)
        {
            WriteError(message, "");
        }

        private static void WriteToDisk(string log)
        {
            try
            {
                if (!File.Exists(logFile))
                {
                    System.IO.StreamWriter LogFile = new System.IO.StreamWriter(logFile);
                    LogFile.Close();
                }
                using (StreamWriter Writer = new StreamWriter(logFile, true))
                {
                    Writer.WriteLine(log);
                    Writer.Close();
                }
            }
            catch { }
        }
    }
}